//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "VerifMaBase.h"
#include <IBDatabase.hpp>
//---------------------------------------------------------------------------
#pragma link "IBScript"
#pragma package(smart_init)

//---------------------------------------------------------------------------
//              CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall CVerifMaBase::CVerifMaBase( void )
{
    MaBase = NULL;
    MaBase = new TIBDatabase( (TComponent*)0);
    MaBase->DatabaseName = BASEKILOUTOU;
    MaBase->LoginPrompt = false;
    MaBase->Params->Add("user_name=sysdba");
    MaBase->Params->Add("password=masterkey");
    try {
        MaBase->Connected = true;
    } catch(EIBInterBaseError &e) {
        ShowMessage("Création d'une nouvelle base");
        if(e.IBErrorCode == isc_io_error) {
            if(CopyFile("basevide.fdb", "kiloutou.fdb", false) != 0) {
                //Creation de la base kiloutou si elle est manquante
                MaBase->Connected = true;
                ExecuterScriptMetadata(SCRIPT_KILOUTOU_BASE);
            }
        }
    }
}
//---------------------------------------------------------------------------
//              DESTRUCTEUR
//---------------------------------------------------------------------------
__fastcall CVerifMaBase::~CVerifMaBase()
{
    MaBase->Connected = false;
    if(MaBase) {delete MaBase; MaBase = NULL; }
}
//---------------------------------------------------------------------------
//              VERIFIER LES TABLES
//---------------------------------------------------------------------------
bool __fastcall CVerifMaBase::VerifierTables( void )
{
    bool bRetour = false;
    if(     IsTablesExist("CLIENT")
        &&  IsTablesExist("LOCATION")
        &&  IsTablesExist("MATERIEL")
        &&  IsTablesExist("CAMION")
        &&  IsTablesExist("PELLETEUSE")
        &&  IsTablesExist("TONDEUSE"))
    {
        bRetour = true;
    }
    else {
        //Creation des tables
        ExecuterScriptMetadata(SCRIPT_KILOUTOU_BASE);
    }
    return bRetour;
}
//---------------------------------------------------------------------------
//              EXISTANCES DES TABLES
//---------------------------------------------------------------------------
bool __fastcall CVerifMaBase::IsTablesExist(const AnsiString &NomTable )
{
    bool bRetour = false;

    TIBTransaction * Transaction = NULL;
    Transaction = new TIBTransaction((TComponent*)0);
    Transaction->DefaultDatabase = MaBase;
    Transaction->Params->Clear();
    Transaction->Params->Add("write");
    Transaction->Params->Add("consistency");
    Transaction->Active = true;

    TIBQuery * MaRequete = NULL;
    MaRequete = new TIBQuery((TComponent*)0);
    MaRequete->Database = MaBase;
    MaRequete->Transaction = Transaction;


    MaRequete->SQL->Text =  (AnsiString)    "select count(rdb$relation_name) "+
                                            "from rdb$relations "+
                                            "where rdb$relation_name = \'"+NomTable+"\'";
    MaRequete->Open();
    MaRequete->First();
    if(MaRequete->Fields->Fields[0]->AsInteger != 0)
    {
        bRetour = true;
    }

    MaRequete->SQL->Clear();
    MaRequete->Close();
    MaRequete->Transaction->Active = false;

    if(Transaction) {delete Transaction; Transaction = NULL;}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}

    return bRetour;
}
//---------------------------------------------------------------------------
//              EXECUTION DU METADATA
//---------------------------------------------------------------------------
void __fastcall CVerifMaBase::ExecuterScriptMetadata(const AnsiString &NomMetadata )
{
    //creation de la base grace au metadata.sql
    TIBTransaction * Transaction = NULL;
    Transaction = new TIBTransaction((TComponent*)0);
    Transaction->DefaultDatabase = MaBase;
    Transaction->Params->Clear();
    Transaction->Params->Add("write");
    Transaction->Params->Add("consistency");
    Transaction->Active = true;

    TIBScript *ibScript = NULL;
    ibScript = new TIBScript((TComponent*)0);
    ibScript->Database = MaBase;
    ibScript->Transaction = Transaction;
    ibScript->Terminator = ";";

    ibScript->Script->LoadFromFile(NomMetadata);

    if(FileExists(NomMetadata)) {
      if(ibScript->ValidateScript()) {
          ibScript->ExecuteScript();
          ibScript->Script->Clear();
      }
    }

    ibScript->Transaction->Active = false;

    if(ibScript) {delete ibScript; ibScript = NULL;}
    if(Transaction) {delete Transaction; Transaction = NULL;}
}
